var grid;
function initGrid(staffRoleId){
  $("#startDate").ligerDateEditor();
  $("#endDate").ligerDateEditor();
  grid = $("#maingrid").ligerGrid({
    url:HTR.U('index/Users/pageQuery'),
    pageSize:HTR.pageSize,
    pageSizeOptions:HTR.pageSizeOptions,
    height:'99%',
        width:'100%',
        minColToggle:6,
        rownumbers:true,
        columns: [
          { display: '姓名', name: 'user_name', isSort: false,width:100},
          { display: '类型', name: 'style', isSort: false,width:50},
          { display: '联系方式', name: 'user_phone', isSort: false,width:150},
          { display: '初次消费金额', name: 'first_consume', isSort: false,width:100},
          { display: '所属地区', name: 'areaPathName', isSort: false,align:'left'},
          { display: '疾病类型', name: 'diseaseName', isSort: false,width:100},
          { display: '客户分类', name: 'classifyName', isSort: false,width:100},
          { display: '消费类型', name: 'consumeName', isSort: false,width:100},
          { display: '客户类型', name: 'type', isSort: false,width:100},
          { display: '消费总金额', name: 'total', isSort: false,width:100},
          { display: '积分', name: 'integral',isSort: false,width:70,render: function (rowdata, rowindex, value){
              return "<a href='" + HTR.U('index/integrallog/index','user_id=' + rowdata['user_id'])+"'>"+rowdata['integral']+"</a> "; 
          }},
          { display: '操作', name: 'op',width:120,isSort: false,render: function (rowdata, rowindex, value){
              var h = "";
              if(HTR.GRANT.HYGL_02 && staffRoleId < 3)h += "<a href='"+HTR.U('index/Users/toEdit','id='+rowdata['user_id'])+"'>修改</a> ";
              if(HTR.GRANT.KCGL_02 && staffRoleId < 3)h += "<a href='javascript:editIntegral(" + rowdata['user_id'] + ")'>积分</a> ";
              if(HTR.GRANT.HYGL_03 && staffRoleId < 3)h += "<a href='javascript:del(" + rowdata['user_id'] + ")'>删除</a> "; 
              return h;
          }}
        ]
    }); 
}

function userQuery(){
	var query = HTR.getParams('.query');
	grid.set('url',HTR.U('index/Users/pageQuery',query));
}


function del(id){
  var box = HTR.confirm({content:"您确定要删除该客户吗?",yes:function(){
   var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
   $.post(HTR.U('index/Users/del'),{id:id},function(data,textStatus){
        layer.close(loading);
        var json = HTR.toIndexJson(data);
        if(json.status=='1'){
            HTR.msg(json.msg,{icon:1});
            layer.close(box);
            grid.reload();
        }else{
            HTR.msg(json.msg,{icon:2});
        }
      });
    }});
}
function editInit(opts){
  $("#user_birthday").ligerDateEditor();
  if(opts.areaId>0){
    var areaIdPath = opts.areaIdPath.split("_");
      $('#area_0').val(areaIdPath[0]);
      var aopts = {id:'area_0',val:areaIdPath[0],childIds:areaIdPath,className:'j-areas',isRequire:true}
    HTR.ITSetAreas(aopts);    
  } 
	 /* 表单验证 */
    $('#userForm').validator({
            rules: {
                myRemote: function(element){
                    return $.post(HTR.U('index/users/checkPhone'),{'user_phone':element.value,'user_id':$('#user_id').val()},function(data,textStatus){});
                }
            },
            fields: {
                user_name: {
                  rule:"required",
                  msg:{required:"请输入姓名"},
                  tip:"请输入姓名",
                  ok:"",
                },                
                user_phone: {
                  rule:"required",
                  msg:{required:"请输入联系方式"},
                  tip:"请输入联系方式",
                  ok:"",
                },                
                // user_address: {
                //   rule:"required",
                //   msg:{required:"请输入详细地址"},
                //   tip:"请输入详细地址",
                //   ok:"",
                // },     
            },
          valid: function(form){
            var params = HTR.getParams('.ipt');
            params.areaId = HTR.ITGetAreaVal('j-areas');
            if(!params.areaId){
              HTR.msg('请选择所属地区',{icon:2});
              return false;
            }
            var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
            $.post(HTR.U('index/Users/'+((params.user_id==0)?"add":"edit")),params,function(data,textStatus){
              layer.close(loading);
              var json = HTR.toIndexJson(data);
              if(json.status=='1'){
                  HTR.msg("操作成功",{icon:1});
                  location.href=HTR.U('Index/Users/index');
              }else{
                    HTR.msg(json.msg,{icon:2});
              }
            });
      }
    });
}


function editIntegral(user_id){
  $('#integralForm')[0].reset();
  var box = HTR.open({title:"调整积分",type:1,content:$('#integralBox'),area: ['460px', '260px'],btn:['确定','取消'],yes:function(){
    $('#integralForm').submit();
            }});
  $('#integralForm').validator({
      fields: {
        num: {
          tip: "请输入数量",
          rule: '数量:required;'
        },
        desc: {
          tip: "请输入描述",
          rule: '描述:required;'
        },
      },
      valid: function(form){
          var params = HTR.getParams('.ipt');
          params.user_id = user_id;
          var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
          $.post(HTR.U('index/integrallog/upIntegral'),params,function(data,textStatus){
              layer.close(loading);
              var json = HTR.toIndexJson(data);
              if(json.status=='1'){
                  HTR.msg(json.msg,{icon:1});
                  layer.close(box);
                  grid.reload();
              }else{
                  HTR.msg(json.msg,{icon:2});
              }
          });
      }
  });
}